package com.invengo.module_check;

import android.content.Context;
import android.os.Bundle;
import android.view.View;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import me.goldze.mvvmhabit.base.BaseFragment;
import me.goldze.mvvmhabit.http.interceptor.logging.LocalLogUtil;
import me.goldze.mvvmhabit.utils.KLog;

public class FragmentLifecycleCallbackImpl extends FragmentManager.FragmentLifecycleCallbacks {
    @Override
    public void onFragmentAttached(FragmentManager fm, Fragment f, Context context) {
//        KLog.i(f.toString() + " - onFragmentAttached");
    }

    @Override
    public void onFragmentCreated(FragmentManager fm, Fragment f, Bundle savedInstanceState) {
//        KLog.i(f.toString() + " - onFragmentCreated");
        // 在配置变化的时候将这个 Fragment 保存下来,在 Activity 由于配置变化重建时重复利用已经创建的 Fragment。
        // https://developer.android.com/reference/android/app/Fragment.html?hl=zh-cn#setRetainInstance(boolean)
        // 如果在 XML 中使用 <Fragment/> 标签,的方式创建 Fragment 请务必在标签中加上 android:id 或者 android:tag 属性,否则 setRetainInstance(true) 无效
        // 在 Activity 中绑定少量的 Fragment 建议这样做,如果需要绑定较多的 Fragment 不建议设置此参数,如 ViewPager 需要展示较多 Fragment
//        f.setRetainInstance(true);
    }

    @Override
    public void onFragmentViewCreated(FragmentManager fm, Fragment f, View v, Bundle savedInstanceState) {
//        KLog.i(f.toString() + " - onFragmentViewCreated");
    }

    @Override
    public void onFragmentActivityCreated(FragmentManager fm, Fragment f, Bundle savedInstanceState) {
//        KLog.i(f.toString() + " - onFragmentActivityCreated");
    }

    @Override
    public void onFragmentStarted(FragmentManager fm, Fragment f) {
//        KLog.i(f.toString() + " - onFragmentStarted");
    }

    @Override
    public void onFragmentResumed(FragmentManager fm, Fragment f) {
//        KLog.e("这里Resumedl 。。。。"+f.getClass().getSimpleName()+ f.getTag());
//        KLog.e(f.toString() + " - onFragmentResumed  1秒后开启自动扫描");
        if (f instanceof BaseFragment) {

//            String messageKey = f + "_" + ((BaseFragment) f).getViewModel().getCacheTag();
//            ((BaseFragment) f).getViewModel().registerEvent( messageKey);
        }
    }

    @Override
    public void onFragmentPaused(FragmentManager fm, Fragment f) {
        LocalLogUtil.temp2File("打开新界面："+f.getClass().getSimpleName());
        //取消订阅的时候已经有了停止扫描
        if (f instanceof BaseFragment) {
//            KLog.e("这里onpaused了。。。。"+f.getClass().getSimpleName()+ f.getTag());
            ((BaseFragment<?, ?>) f).getViewModel().removeEvent();
        }
    }

    @Override
    public void onFragmentStopped(FragmentManager fm, Fragment f) {
        if (f instanceof BaseFragment) {
//            System.out.println("onFragmentStopped  ==>" + f);
            ((BaseFragment) f).getViewModel().getUC().getHideKeyboardEvent().call();
        }
    }

    @Override
    public void onFragmentSaveInstanceState(FragmentManager fm, Fragment f, Bundle outState) {
//        KLog.i(f.toString() + " - onFragmentSaveInstanceState");
    }

    @Override
    public void onFragmentViewDestroyed(FragmentManager fm, Fragment f) {
//        KLog.i(f.toString() + " - onFragmentViewDestroyed");
    }

    @Override
    public void onFragmentDestroyed(FragmentManager fm, Fragment f) {
//        KLog.i(f.toString() + " - onFragmentDestroyed");
    }

    @Override
    public void onFragmentDetached(FragmentManager fm, Fragment f) {
//        KLog.i(f.toString() + " - onFragmentDetached");
    }
}
